package com.hexus.eve.actions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

public class TypesAutocompleter extends ActionSupport{
	private static final long serialVersionUID = 1L;
	
	private Map<String,String> json;
	private String inputType;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public String getOptions() {
		json = new HashMap<String,String>();
		
		if(inputType != null && !"".equals(inputType)) {
			Session session = sessionFactory.openSession();
			@SuppressWarnings("unchecked")
			List<Object[]> items = 
				session.createSQLQuery("select typeID, typeName from TypesAutocomplete where typeName like :inputType")
				.addScalar("typeID", Hibernate.INTEGER)
				.addScalar("typeName", Hibernate.STRING)
				.setString("inputType", inputType + '%')
				.list();
			
			for (Object[] item: items){
				json.put((String)item[1], ((Integer)item[0]).toString());
			}
		}
		
		return SUCCESS;
	}
	
	public void setInputType(String inputType) {
		this.inputType = inputType;
	}

	public Map<String, String> getJson() {
		return json;
	}
	
}
